let app = getApp()

var timer = null
Page({
  /**
   * 页面的初始数据
   */
  data: {
    colorList: ['red', 'black', 'green', 'yellow', 'blue', 'purple'],
    bgcList: ['white', 'pink', 'black', 'gray', 'skyblue', 'yellow'],
    str: '测试文字adsaldksjdhkjdhcbxxmzcbzmo到家啦的空间了解啦',
    isShow: false,
    fontSize: 100,
    maxFontSize: app.globalData.maxFontSize,
    fontColor: 'black',
    bgcColor: 'white',
    duration: 12,
    rate: 200,
    isBold: false,
    istwinkle: false,
    isIpad: app.globalData.isIpad,
    statusBarHeight: app.globalData.statusBarHeight,
    animationData: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setKeepScreenOn({
      keepScreenOn: true
    })
  },
  twinkleFun(flag = true) {
    let colorList = this.data.colorList;
    let index = 0;
    if (flag) {
      this.setData({
        istwinkle: !this.data.istwinkle
      })
    }
    this.endSetInter()
    if (this.data.istwinkle) {
      timer = setInterval(() => {
        if (index < colorList.length - 1) {
          index++
        } else {
          index = 0
        }
        console.log(index)
        this.setData({
          fontColor: colorList[index]
        })
      }, this.data.rate)
    }
  },

  endSetInter() {
    let that = this;
    //清除计时器  即清除setInter
    clearInterval(timer)
    timer = null
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  getVal(e) {
    this.setData({
      str: e.detail.value,
    })
  },
  preview() {
    let that = this
    that.setData({
      animationData: ''
    })
    that.setData({
      isShow: true,
      animationData: that.data.isIpad ? `${that.data.duration}s wordsLoopForIpad linear infinite normal` : `${that.data.duration}s wordsLoopForAndroid linear infinite normal`
    })
  },
  swithIsshow() {
    this.setData({
      isShow: !this.data.isShow,
      animationData: ''
    })
  },
  sliderHandleSize(e) {
    this.setData({
      fontSize: e.detail.value,
    })
  },
  sliderHandleDuration(e) {
    this.setData({
      animationData: '',
      duration: e.detail.value,
    })
  },
  sliderHandleRate(e) {
    this.setData({
      animationData: '',
      rate: e.detail.value,
    })
    this.twinkleFun(false)
  },
  chooseColor(e) {
    this.setData({
      fontColor: e.currentTarget.dataset.color,
    })
  },
  chooseBgcColor(e) {
    this.setData({
      bgcColor: e.currentTarget.dataset.color,
    })
  },
  chooseBold(e) {
    this.setData({
      isBold: !this.data.isBold,
    })
  },
  reset() {
    this.setData({
      animationData: ''
    })
  }
})